#ifndef LOGGER_H
#define LOGGER_H

#include <QObject>
#include <QMutex>
#include <QFile>
#include <QDateTime>
#include <QApplication>
#include "logworker.h"

/*
    日志记录器
*/
class Logger : public QObject
{
    Q_OBJECT
public:
    //日志等级,级别依次升高
    enum LogLevel
    {
        Debug,
        Info,
        Warn,
        Error
    };
    Q_ENUM(LogLevel)

    //创建日志对象
    static Logger* createLogger(QObject *parent,const QString& moduleName,
                         Logger::LogLevel level= Logger::LogLevel::Debug,
                         bool enable=true);
public:    
    explicit Logger(QObject *parent = nullptr);
    virtual ~Logger();

    LogLevel logLevel();
    void setLogLevel(LogLevel level);
    bool enable();
    void setEnable(bool enable);

    void log(LogLevel level,const QString& text);
    void debug(const QString& text);
    void info(const QString& text);
    void warn(const QString& text);
    void error(const QString& text);

signals:
    void sigOpen();
    void sigWrite(const QString& text);

private:
    void open();
    void close();
    void backup();
    void write(const QString& level, const QString& text);

private:
    bool m_enable;
    LogLevel m_level;
    LogWorker* m_logWorker;
    QThread* m_thread;

    friend Logger* createLogger(QObject *parent,const QString& moduleName,
                                Logger::LogLevel level= Logger::LogLevel::Debug,
                                bool enable=true);
};

#endif // LOGGER_H
